<%@language=VBScript%>
<%
	Session("FormName")="F4"
%>
<!--#include file="..\CheckSession.inc"-->
<!--#include file=..\getFormName.inc-->
<!--#include file="..\RootContent.inc"-->
<HTML>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<%	
	sSQL="Select * from config"
	Set RecC=server.createObject("ADODB.Recordset")
	RecC.CursorType=3
	RecC.CacheSize=5
	RecC.open sSQL,Conn
	if RecC.recordcount  then
		bDatabaseN=recC("Databasename")
	end if
	RecC.close
	Set recC=nothing
	
	Path=server.MapPath("..\BackupDatabase")
	if request("bok")="ok" then
		On error resume next
		Filename=Request("ComboFilename")
		PathFilename=Path & "\" & Filename
		sSQL="Use " & Application("DataBaseName")
		Conn.Execute sSQL
		sSQL="Use Pubs Restore database "& bDatabaseN &" from disk='"& PathFilename &"' WITH RESTART"
		Conn.Execute sSQL
		sSQL="Use " & Application("DataBaseName")
		Conn.Execute sSQL
		'restore file noidung
		'sSQL="Select * from tblbackup where projectname='"& TenFile(Filename) &"'"
		sSQL="select * from tblbackup where id <= (select id from tblprojectbackup where projectname = '"& TenFile(Filename) &"')"
		Set RecRestore=server.CreateObject("ADODB.Recordset")
		RecRestore.CursorType=3
		RecRestore.CacheSize=5
		RecRestore.Open sSQL,Conn
			set bfile=Server.CreateObject("ActiveFile.File")
			do while Not RecRestore.EOF	
				bfile.Name=RootContent & RecRestore("pathname")
				if not bfile.Exists() then
					bfile.Name=RootBackup & "NOIDUNG\" & RecRestore("pathname")
					bfile.Copy(RootContent & RecRestore("pathname"))
				else
					bfile.Name=RootBackup & "NOIDUNG\" & RecRestore("pathname")
					bngaymodibackup=bfile.LastModified
					bfile.Name=RootContent & RecRestore("pathname")
					bngaymodiRootContent=bfile.LastModified
					if bngaymodibackup <> bngaymodiRootContent then
						bfile.Name=RootBackup & "NOIDUNG\" & RecRestore("pathname")
						bfile.Copy(RootContent & RecRestore("pathname"))
					end if
				end if
				RecRestore.MoveNext
			loop
			Set bfile=Nothing
		RecRestore.close
		Set RecRestore=Nothing
		if Err.number=0 then
			bmsg=true
			bTrangthailog=1
		else
			bmsg=false
			bTrangthailog=0
		end if
		'Ghi nhan Ai da login thanh cong						
		bHoten=Session("bHoten")
		bNgaygio=cstr(Date()) &" "& cstr(Time())
		bChucnang="Kh&#244;i ph&#7909;c CSDL"
		bIDTailieu=""
		bDongtac="Kh&#244;i ph&#7909;c"
		sSQL="insert into loguser(Tennguoisudung,ngaygio,chucnangdung,dongtac,IDTailieu,Trangthai) Values('"& bHoten _
			&"','"& bNgaygio &"','"& bChucnang &"','"& bDongtac &"','"& bIDTailieu &"','"& bTrangthailog &"')"
		Conn.Execute sSQL
	end if
%>	
<Script language=vbscript runat=server>
Function TenFile(filename)
	dim longfilename
	dim VtCham
	longfilename=len(filename)
	VtCham=instr(1,filename,".")
	if VtCham>0 then
		TenFile=mid(filename,1,VtCham-1)
	end if
End Function
</SCRIPT>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function CmdOK_onclick() {
	if(frm.TxtCSDLSaoLuu.value==""){
		alert("B&#7841;n c&#7847;n ph&#7843;i cho t&#234;n CSDL");
		frm.TxtCSDLSaoLuu.focus;
		return false;
	}
	if(frm.ComboFilename.value==""){
		alert("Cơ sở dữ liệu khôi phục chưa có. Để có CSDL khôi phục, bạn hãy chạy chức năng Sao lưu dữ liệu");
		return false;
	}	
	f=frm.ComboFilename.value;
	window.frm.action="restore.asp?bok=ok&Filename="+f;
	window.frm.submit();
}
function CmdExit_onclick() {
	window.frm.action="../leftmain.asp";
	window.frm.target="main"
	window.frm.submit();
}

function SetFocus(){
	window.frm.ComboFilename.focus();
}
//-->
</SCRIPT>
</HEAD>
<BODY bgcolor=#F8F9D9 style="font-family:Times new roman" onload="SetFocus()">
&nbsp;
&nbsp;
&nbsp;
<Div align=center>
<%if request("bok")="ok" then
	if not bmsg then
		%><font color=red>Lỗi khôi phục dữ liệu</font><%
	else
		%><font color=blue>Dữ liệu đã được khôi phục</font><%
	end if
  end if
%>
<form name=frm method=post >
<TABLE border=0 cellSpacing=1 width=500 bgcolor="#bdd5d0" style="BORDER-BOTTOM-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-RIGHT-STYLE: solid;Border-color:#C0C0C0; BORDER-TOP-STYLE: solid">
    <TR>
        <TD COLSPAN=3 WIDTH=500 BGCOLOR="#000080" ALIGN=CENTER>
        <font face="TAHOMA" size="2" color="#FFFFFF">XÁC NHẬN CÁC LỰA CHỌN</font></TD>
    </TR>
    <TR>
        <TD width="30%"><label for=S1 accesskey="L"><p><u>L</u>ấy cơ sở dữ liệu từ</p></label></TD>
        <TD width="70%">
        <%'<INPUT name=TxtCSDLNguon readonly size=47 >%>
        <%
			Set Dir = Server.CreateObject("ActiveFile.Directory")
			Dir.Path =Path
			dir.SortOrder=2
        %>
			<select size="1" name="ComboFilename" style="height: 23; position: relative; width:100%" id=S1>
				<%for each fileobj In dir.files
					If not fileObj.IsDirectory Then	%>		
						<option value='<%=fileObj.FileName%>'><%=trim(fileObj.FileName)%>
					<%end if%>
				<%next%>
			</select>
        </TD>
    </TR>
    <TR>
        <TD width="30%">Tới cơ sở dữ liệu</TD>
        <TD width="70%">
        <textarea name=TxtCSDLSaoLuu readonly style="width:100%;background-color: #DFDFDF; font-family:Arial; font-size:10pt" rows=1 cols="20"><%=trim(bDatabaseN)%></textarea></TD>      
    </TR>
    <TR>
        <TD width="30%">Ðịa chỉ khôi phục</TD>
        <TD width="70%">
            <textarea name=TxtDiachitoi readonly style="width:100%;background-color: #DFDFDF; font-family:Arial; font-size:10pt"  rows=1 cols="20"><%=server.MapPath("..\BackupDatabase\")%></textarea></TD>
    </TR>
    <TR>
        <TD width="100%" colspan=2 align=center>
			<Button name=CmdOK style="HEIGHT: 25px; WIDTH: 80px;font-family:Arial" LANGUAGE=javascript onclick="return CmdOK_onclick()" accesskey="G"><p><u>G</u>hi nhận</p></Button>
	        <Button name=CmdExit style="HEIGHT: 25px; WIDTH: 80px;font-family:Arial" LANGUAGE=javascript onclick="return CmdExit_onclick()" accesskey="Q"><p><u>Q</u>uay lại</p></Button></TD>
    </TR>
</TABLE>
</form>
</Div>
</BODY>
</HTML>